package com.test.rm.service.impl;

import com.test.rm.mapper.AtMapper;
import com.test.rm.service.AtService;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Service
public class AtServiceImpl implements AtService {

    @Autowired
    private AtMapper atMapper;

    AtomicInteger ai = new AtomicInteger(0);

    @Override
    @Transactional
    public String insert(Map<String, String> params) {
        log.info("------------------> xid = " + RootContext.getXID());
        atMapper.insert(params);
        if(ai.getAndIncrement()%3==0){
            throw new RuntimeException("rm: " + RootContext.getXID());
        }
        return "success";
    }
}
